from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import Column, String, Integer, Text
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from faker import Factory

engine = create_engine('mysql+mysqldb://abc:Abc123..@192.168.88.200:3306/students')
Base = declarative_base()


class User2(Base):
    __tablename__ = "users2"

    id = Column(Integer, primary_key=True)
    username = Column(String(64), nullable=False, index=True)
    password = Column(String(64), nullable=False)
    email = Column(String(64), nullable=False, index=True)
    articles = relationship('Article', backref='author')
    userinfo = relationship('UserInfo', backref='user', uselist=False)

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.username)


# Base.metadata.create_all(engine)

class Article(Base):
    __tablename__ = "articles"
    id = Column(Integer, primary_key=True)
    title = Column(String(255), nullable=False, index=True)
    content = Column(Text)
    user_id = Column(Integer, ForeignKey('users2.id'))

    # author = relationship('User2')

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.title)


class UserInfo(Base):
    __tablename__ = "userinfos"

    id = Column(Integer, primary_key=True)
    name = Column(String(55))
    qq = Column(String(11))
    phone = Column(String(11))
    link = Column(String(100))
    user_id = Column(Integer, ForeignKey("users2.id"))


if __name__ == '__main__':
    Base.metadata.create_all(engine)
